﻿/* 
 PureMVC C# Port by Andy Adamczak <andy.adamczak@puremvc.org>, et al.
 PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved. 
 Your reuse is governed by the Creative Commons Attribution 3.0 License 
*/

#region Using

using System;
using System.Reflection;

using PureMVC.Interfaces;

#endregion

namespace PureMVC.Patterns
{
	//Observer观察者
	public class Observer : IObserver
	{
		private string m_notifyMethod;

		private object m_notifyContext;

		protected readonly object m_syncRoot = new object();
		public Observer(string notifyMethod, object notifyContext)
		{
			m_notifyMethod = notifyMethod;
			m_notifyContext = notifyContext;
		} 
		public virtual void NotifyObserver(INotification notification)
		{
			object context;
			string method;
			 
			lock (m_syncRoot)
			{
				context = NotifyContext;
				method = NotifyMethod;
			}

			Type t = context.GetType();
			BindingFlags f = BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase;
			MethodInfo mi = t.GetMethod(method, f);
			mi.Invoke(context, new object[] { notification });
		} 
		public virtual bool CompareNotifyContext(object obj)
		{
			lock (m_syncRoot)
			{ 
				return NotifyContext.Equals(obj);
			}
		} 
		public virtual string NotifyMethod
		{
			private get
			{ 
				return m_notifyMethod;
			}
			set
			{ 
				m_notifyMethod = value;
			}
		} 
		public virtual object NotifyContext
		{
			private get
			{ 
				return m_notifyContext;
			}
			set
			{ 
				m_notifyContext = value;
			}
		} 
		 
	}
}
